package com.ohuang.wallpapermanager.vm

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.ohuang.wallpapermanager.compose.update
import com.ohuang.wallpapermanager.config.LocalImageManager
import com.ohuang.wallpapermanager.repository.WallPaperInfoRepository
import com.ohuang.wallpapermanager.screen.EditWallPaperInfoScreenEvent
import com.ohuang.wallpapermanager.screen.EditWallPaperInfoState
import com.ohuang.wallpapermanager.screen.toData
import com.ohuang.wallpapermanager.screen.toState
import com.ohuang.wallpapermanager.util.AppUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class EditWallPaperInfoViewModel : ViewModel() {

    private val _editWallPaperInfoState = mutableStateOf(EditWallPaperInfoState())
    val editWallPaperInfoState: State<EditWallPaperInfoState> = _editWallPaperInfoState
    private var wallPaperInfoRepository: WallPaperInfoRepository? = null
    suspend fun init(isLock: Boolean, id: Long) {
        withContext(Dispatchers.IO) {
            wallPaperInfoRepository = WallPaperInfoRepository(isLock)
            if (id != 0L) {
                val findWallPaperInfo = wallPaperInfoRepository?.findWallPaperInfo(id)
                if (findWallPaperInfo != null) {
                    _editWallPaperInfoState.value = findWallPaperInfo.toState()
                }
            }
        }
    }

    fun updateUri(uri: String){
        _editWallPaperInfoState.update {
            it.copy(uri = uri)
        }
    }

    suspend fun saveWallPaperInfo() {
        if (wallPaperInfoRepository==null){
            AppUtil.getInstance().toast("操作失败")
            return
        }
        _editWallPaperInfoState.update {
            it.copy(isLoading = true)
        }
        withContext(Dispatchers.IO) {
            val value = _editWallPaperInfoState.value
            val newFile = copyImage(value.uri)
            val oldFile = value.filePath
            val newData = if (newFile.isNotEmpty()) {
                value.copy(filePath = newFile)
            } else {
                value
            }
            wallPaperInfoRepository?.addWallPaperInfo(newData.toData())
            if (newFile.isNotEmpty() && oldFile.isNotEmpty()) {
                File(oldFile).delete()
            }
        }
    }

    suspend fun deleteWallPaperInfo() {
        if (wallPaperInfoRepository==null){
            AppUtil.getInstance().toast("操作失败")
            return
        }
        _editWallPaperInfoState.update {
            it.copy(isLoading = true)
        }
        withContext(Dispatchers.IO) {
            val value = _editWallPaperInfoState.value
            val oldFile = value.filePath
            wallPaperInfoRepository?.deleteWallPaperInfo(value.id)
            if (oldFile.isNotEmpty()) {
                File(oldFile).delete()
            }
        }
    }


    private fun copyImage(uri: String): String {
        if (uri.isNotEmpty()) {
            return LocalImageManager.saveImageForUri(uri)
        }
        return ""
    }




}